package com.suep.game_chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suep.game_chat.entity.User;
import com.suep.game_chat.entity.UserExperience;
import com.suep.game_chat.mapper.UserExperienceMapper;
import com.suep.game_chat.mapper.UserMapper;
import com.suep.game_chat.service.ExperienceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 经验值服务实现类
 */
@Service
public class ExperienceServiceImpl extends ServiceImpl<UserExperienceMapper, UserExperience> implements ExperienceService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserExperienceMapper userExperienceMapper;

    /**
     * 添加经验值
     * @param userId 用户ID
     * @param points 经验值点数
     * @param reason 原因
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean addExperience(String userId, int points, String reason) {
        // 1. 获取用户信息
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }

        // 2. 创建经验值记录
        UserExperience experience = new UserExperience();
        experience.setUserId(userId);
        experience.setPoints(points);
        experience.setReason(reason);
        experience.setCreateTime(LocalDateTime.now());
        experience.setUpdateTime(LocalDateTime.now());

        // 3. 保存经验值记录
        int result = userExperienceMapper.insert(experience);
        if (result <= 0) {
            return false;
        }

        // 4. 更新用户总经验值
        user.setExperience(user.getExperience() + points);
        // 5. 检查并更新用户等级
        updateUserLevel(user);
        
        return userMapper.updateById(user) > 0;
    }

    /**
     * 获取用户经验值历史
     * @param userId 用户ID
     * @return 经验值历史记录列表
     */
    @Override
    public List<UserExperience> getUserExperienceHistory(String userId) {
        LambdaQueryWrapper<UserExperience> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserExperience::getUserId, userId)
                .orderByDesc(UserExperience::getCreateTime);
        return userExperienceMapper.selectList(wrapper);
    }

    /**
     * 获取用户当前等级
     * @param userId 用户ID
     * @return 用户等级
     */
    @Override
    public int getUserLevel(String userId) {
        User user = userMapper.selectById(userId);
        return user != null ? user.getLevel() : 0;
    }

    /**
     * 获取用户当前经验值
     * @param userId 用户ID
     * @return 用户经验值
     */
    @Override
    public int getUserExperience(String userId) {
        User user = userMapper.selectById(userId);
        return user != null ? user.getExperience() : 0;
    }

    /**
     * 更新用户等级
     * @param user 用户对象
     */
    private void updateUserLevel(User user) {
        int currentLevel = user.getLevel();
        int experience = user.getExperience();
        
        // 根据经验值计算新等级
        int newLevel = calculateLevel(experience);
        
        // 如果等级提升，更新用户等级
        if (newLevel > currentLevel) {
            user.setLevel(newLevel);
        }
    }

    /**
     * 根据经验值计算等级
     * @param experience 经验值
     * @return 等级
     */
    private int calculateLevel(int experience) {
        // 简单的等级计算公式：每1000经验值提升1级
        return (experience / 1000) + 1;
    }
} 